package com.example.yamba;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import com.sina.weibo.sdk.openapi.models.Status;

/**
 * Created by jagery on 8/25/15.
 */
public class TimelineActivity1 extends Activity{
    private static final String TAG = TimelineActivity1.class.getSimpleName();
    ListView listView ;
    SimpleCursorAdapter adapter;
    static String[] FROM = {StatusData.C_ID,StatusData.C_CREATE_AT,StatusData.C_STATUS};
    static int[] TO = {R.id.textUser,R.id.textCreateAt,R.id.text};
    private StatusData statusData ;
    private YambaApplication yamba;
    private Cursor cursor;
    protected void onCreate(Bundle bundle){
       super.onCreate(bundle);
        setContentView(R.layout.timeline1);
        listView = (ListView)findViewById(R.id.listViewTime);
        yamba = (YambaApplication)getApplication();
        statusData = yamba.getStatusData();

        Log.i(TAG, "onCreated");
    }
    protected void onResume(){
        super.onResume();
        cursor = statusData.getStatusUpdates();

        adapter = new SimpleCursorAdapter(this, R.layout.row,cursor,FROM,TO,0);
        adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
            @Override
            public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                String json = cursor.getString(cursor.getColumnIndex(StatusData.C_STATUS));
                Status status = Status.parse(json);
                switch (view.getId()) {
                    case R.id.textUser:
                        ((TextView) view).setText(status.user.screen_name);
                        return true;
                    case R.id.textCreateAt:
                        ((TextView) view).setText(status.created_at.substring(0, 19));
                        return true;
                    case R.id.text:
                        ((TextView) view).setText(status.text);
                        return true;
                    default:
                        return false;
                }

            }
        });
        listView.setAdapter(adapter);
        BroadcastReceiver receiver = new TimelineREcevier();
        IntentFilter filter = new IntentFilter("com.example.yamba.NEW_STATUS");
        registerReceiver(receiver,filter);
        Log.i(TAG,"register the receiver");
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        // super.onPrepareOptionsMenu(menu);
        if (!menu.hasVisibleItems()) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.status_action_menu, menu);
            Log.d(TAG, "create menu done");
        } else {
            Log.d(TAG, "menu size" + menu.size());
        }
        return super.onPrepareOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) { //
            case R.id.ToggleService:
                if(yamba.isRunning()) {
                    stopService(new Intent(this, UpdateService.class));
                    item.setIcon(android.R.drawable.ic_media_play);
                    Log.d(TAG, "stop the updataservice ");
                }else{
                    startService(new Intent(this, UpdateService.class));
                    item.setIcon(android.R.drawable.ic_media_pause);
                    Log.d(TAG, "start the updataservice");
                }
                break;
            case R.id.actionStatus:
                startActivity(new Intent(this,StatusActivity5.class));
                Log.d(TAG, "start StatusActivity5");
        }
        return true;
    }

    class TimelineREcevier extends BroadcastReceiver{
        @Override
            public void onReceive(Context context, Intent intent) {
            cursor = statusData.getStatusUpdates();
            adapter.swapCursor(cursor);
            int i = intent.getIntExtra("com.example.yamba.NEW_STATUS_COUNT",0);
            Log.i(TAG,"wrap new Cursor  "+i);
        }
    }
}
